﻿#region

using System;
using System.Reflection;
using EsbMonitorMessages.ServiceDescription;

#endregion

namespace MessageTypeDescriber.Xml.Members
{
    public abstract class MemberDescription : IEquatable<MemberDescription>, IMember
    {
        public string Name { get; set; }
        public TypeDescription ReturnType { get; set; }
        public string Description { get; set; }

        public abstract string MemberType { get; }
        public abstract string Signature { get; }

        protected MemberDescription(MemberInfo memberInfo)
        {
            if (memberInfo != null)
            {
                Description[] description = (Description[]) memberInfo.GetCustomAttributes(typeof (Description), false);

                if (description.Length > 0)
                {
                    Description = description[0].Text;
                }
            }
        }

        public bool Equals(MemberDescription other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return Equals(other.Name, Name) && Equals(other.ReturnType, ReturnType);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof (MemberDescription))
            {
                return false;
            }
            return Equals((MemberDescription) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Name != null ? Name.GetHashCode() : 0)*397) ^ (ReturnType != null ? ReturnType.GetHashCode() : 0);
            }
        }

        public static bool operator ==(MemberDescription left, MemberDescription right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(MemberDescription left, MemberDescription right)
        {
            return !Equals(left, right);
        }
    }
}